package com.vcredit.creditcat.manager;

import com.alibaba.dubbo.config.annotation.Service;
import com.vcredit.creditcat.api.IPrivilegeCatManager;
import com.vcredit.creditcat.dto.catCard.LotteryDto;
import com.vcredit.creditcat.dto.catfood.CustCatfoodLogDto;
import com.vcredit.creditcat.model.CatfoodTypeName;
import com.vcredit.creditcat.model.Cust;
import com.vcredit.creditcat.model.LotteryPond;
import com.vcredit.creditcat.model.TextConstant;
import com.vcredit.creditcat.service.catfood.CatfoodService;
import com.vcredit.creditcat.service.privilegeCat.LotteryPondService;
import com.vcredit.framework.annotation.Log;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * Created by liaoshengjun on 2017/12/12.
 */
@Service(protocol = { "protocol1" }, version = "1.0")
public class PrivilegeCatManager implements IPrivilegeCatManager {
	@Autowired
	private LotteryPondService lotteryPondService;

	@Autowired
	private CatfoodService catfoodService;

	/**
	 * 获取抽奖数据
	 * @return
	 */
	@Override
	@Log
	public LotteryDto getLotteryData(){
		LotteryDto lotteryDto=new LotteryDto();
		CatfoodTypeName catfoodTypeName=catfoodService.queryCustCatfoodTypeName(3L);
		lotteryDto.setConsume(catfoodTypeName.getDefaultValue());
		List<String> prizes=new ArrayList<>();
		lotteryDto.setPrizes(prizes);
		List<LotteryPond> lotteryPondList=lotteryPondService.findByLotteryType(0);
		for(LotteryPond lotteryPond:lotteryPondList){
			StringBuilder PrizeBuilder=new StringBuilder();
			if(lotteryPond.getPrizeType().equals(1)){
				PrizeBuilder=PrizeBuilder.append(TextConstant.PRIZE_TYPE_1).append(lotteryPond.getPrizeNum()).append(TextConstant.FEN);
			}else if(lotteryPond.getPrizeType().equals(2)){
				PrizeBuilder=PrizeBuilder.append(TextConstant.PRIZE_TYPE_2).append(lotteryPond.getPrizeNum()).append(TextConstant.YUNA);
			}else{
				PrizeBuilder=PrizeBuilder.append(TextConstant.PRIZE_TYPE_0);
			}
			prizes.add(PrizeBuilder.toString());
		}
		return lotteryDto;
	}

	/**
	 * 获取抽奖结果
	 * @param cust
	 * @return
	 * @throws Exception
	 */
	@Override
	@Log
	public Object getLotteryResult(Cust cust) throws Exception{
		Random r=new Random();
		Integer result;
		if(r.nextInt(10)%2==0){
			result=2;
		}else{
			result=6;
		}
		CatfoodTypeName catfoodTypeName=catfoodService.queryCustCatfoodTypeName(3L);
		catfoodService.updateCustCatfood(Long.valueOf(catfoodTypeName.getDefaultValue()),3L,cust);
		return result;
	}
	@Override
	public boolean canSignInToday(Cust cust) {
		return catfoodService.canSignInToday(cust);
	}


	/**
	 *
	 * 更新猫粮日志
	 * */
	@Override
	public boolean updateCustCatfood(Long typeId, Cust cust){
		try{
			CatfoodTypeName catfoodTypeName=catfoodService.queryCustCatfoodTypeName(typeId);
			catfoodService.updateCustCatfood((long)catfoodTypeName.getDefaultValue(),typeId,cust);
			return true;
		}catch(Exception ex){
			return false;
		}
	}
	
	
	/**
	 * 
	 * 当前剩余多少猫粮
	 * */
	@Override
	public Long queryCustRemainCatfood(Cust cust) {
		// TODO Auto-generated method stub
		return catfoodService.queryCustRemainCatfood(cust);
	}
	
	/**
	 * 
	 * 查询猫粮数据列表
	 * */
	
	@Override
	public List<CustCatfoodLogDto> queryCustCatfoodLog(Cust cust) {
		// TODO Auto-generated method stub
		return  catfoodService.queryCustCatfoodLog(cust);
	}
}
